package com.jinduplan.planmanager.ui.activity.project

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.cxz.kotlin.baselibs.adapter.CustomRecyclerAdapter
import com.cxz.kotlin.baselibs.adapter.ViewHolder
import com.cxz.kotlin.baselibs.base.BaseMvpActivity
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.bean.DataProjectBean
import com.jinduplan.planmanager.mvp.contract.ProjectContract
import com.jinduplan.planmanager.mvp.presenter.ProjectPresenter
import com.jinduplan.planmanager.utils.DateUtils
import com.jinduplan.planmanager.utils.FormatterEnum
import com.jinduplan.planmanager.utils.ResUtil
import kotlinx.android.synthetic.main.activity_project.*


class ProjectActivity : BaseMvpActivity<ProjectContract.View, ProjectContract.Presenter>(),
    ProjectContract.View {


    override fun createPresenter(): ProjectContract.Presenter = ProjectPresenter()


    private var projectList: ArrayList<DataProjectBean.DataBean.ProjectBean>? = null
    private var projectBeanList: ArrayList<DataProjectBean.DataBean>? = null

    override fun start() {
        projectList = ArrayList()
        projectBeanList = ArrayList()

        recyclerView.adapter = object : CustomRecyclerAdapter<DataProjectBean.DataBean>(
            this, R.layout.item_project, projectBeanList
        ) {
            override fun convert(
                holder: ViewHolder?,
                t: DataProjectBean.DataBean?,
                position: Int
            ) {

                holder?.setText(R.id.tvAddress, t?.project?.project_name)
                holder?.setText(R.id.tvAddressDetail,"地址:${t?.project?.project_addr}")
                holder?.setText(R.id.tvDate, t?.completion_date)
                holder?.setOnClickListener(R.id.tv_look, View.OnClickListener {
                    val intent = Intent(applicationContext, ProjectDetailActivity::class.java)
                    intent.putExtra("project_name", projectBeanList!![position].project.project_name)
                    intent.putExtra("position", position)
                    intent.putExtra("project_id",projectBeanList!![position].project_id)
                    intent.putExtra("audit_id", projectBeanList!![position].audit_id)
                    startActivity(intent)
                })
            }

        }
        recyclerView.layoutManager = LinearLayoutManager(this)

        swipeRefreshLayout.setOnRefreshListener {
            mPresenter?.getProjectList(1, 500,0,0,1,0)
        }
        mPresenter?.getProjectList(1, 500,0,0,1,0)
    }

    override fun initView() {
        super.initView()
        setStatusBarColor(ResUtil.getC(R.color.white))
        back.setOnClickListener { finish() }
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_project
    }

    override fun setProjectListData(projectData: DataProjectBean) {
        swipeRefreshLayout.isRefreshing = false
        projectList?.clear()
        projectBeanList?.clear()
        projectBeanList?.addAll(projectData.data)
        if (projectData.data.isNotEmpty()) {
            for (index in projectData.data.indices) {
                projectList?.add(projectData.data[index].project)
            }
        }
        recyclerView.adapter?.notifyDataSetChanged()
    }

    override fun readMsgSuccess() {

    }

    override fun auditMsgSuccess() {

    }


}